using System;
using System.Collections.Generic;
using System.Text;

namespace csammisrun.OscarLib.Utility
{
  class ChatExchangeInfo
  {
    public string Name = "";
    public ushort Flags = 0x0000;
    public ushort Exchange = 0x0000;
    public byte CreationPermissions = 0x00;
    public ushort ClassPermissions = 0x0000;

    public string CharSet1 = "";
    public string Language1 = "";
    public string CharSet2 = "";
    public string Language2 = "";
  }
}

namespace csammisrun.OscarLib
{
  /// <summary>
  /// Describes the properties of an OSCAR chat room
  /// </summary>
  public class ChatRoomInfo
  {
    /// <summary>
    /// The fully-qualified name of the chat room
    /// </summary>
    public string FullName = "";
    /// <summary>
    /// The name of the chat room that is fit to display to the client
    /// </summary>
    public string DisplayName = "";
    /// <summary>
    /// The exchange number of the chat room
    /// </summary>
    /// <remarks>Ideally, neither the exchange nor the <see cref="Instance"/> number
    /// should be exposed to the user</remarks>
    public ushort Exchange = 0x0000;
    /// <summary>
    /// The instance number of the chat room
    /// </summary>
    /// <remarks>Ideally, neither the instance nor the <see cref="Exchange"/> number
    /// should be exposed to the user</remarks>
    public ushort Instance = 0x0000;

    /// <summary>
    /// The creation time of the chat room
    /// </summary>
    public uint CreationTime = 0x00000000;
    /// <summary>
    /// A value indicating whether or not new chat rooms can be created
    /// </summary>
    public byte CreationPermissions = 0x00;
    /// <summary>
    /// The flags of the chat room
    /// </summary>
    public ushort Flags = 0x0000;
    /// <summary>
    /// The maximum message length that can be sent to the room
    /// </summary>
    public ushort MaxMessageLength = 0x0000;
    /// <summary>
    /// The maximum number of users that can join the chat room
    /// </summary>
    public ushort MaxOccupants = 0x0000;

    /// <summary>
    /// Converts a fully-qualified chat room name to a displayable name
    /// </summary>
    /// <param name="fullname">A fully-qualified chat room name</param>
    /// <returns>A name that can be displayed to clients</returns>
    public static string GetDisplayName(string fullname)
    {
      StringBuilder retval = new StringBuilder();
      int dash = fullname.IndexOf('-');
      if(dash == -1) return "";
      dash = fullname.IndexOf('-', dash + 1);
      if(dash == -1) return "";
      string name = fullname.Substring(dash + 1);

      for(int i = 0; i < name.Length; i++)
      {
        if(name[i] != '%')
        {
          retval.Append(name[i]);
        }
        else
        {
          string hex = name.Substring(++i, 2);
          i++;
          int result = 0;
#if WindowsCE
          result = Int32.Parse(hex, System.Globalization.NumberStyles.HexNumber, null);
#else
          Int32.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out result);
#endif
          retval.Append(result.ToString());
        }
      }

      return retval.ToString();
    }

    internal csammisrun.OscarLib.Utility.ChatConnection Connection;
  }
}
